package com.feiyu.blog.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyu.blog.entity.Blog;
import com.feiyu.blog.service.BlogService;
import com.feiyu.common.controller.BaseController;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/blog")
@AllArgsConstructor
public class BlogController extends BaseController<BlogService,Blog> {

    private BlogService blogService;

    @GetMapping("/detail/{id}")
    public R<Blog> detail(@PathVariable long id){
        return R.data(blogService.getById(id));
    }

    @GetMapping("/list/{id}")
    public R<List<Blog>> list(@PathVariable long id){
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq("ownerId",id);
        return R.data(blogService.list(wrapper));
    }


    @PostMapping("/add")
    public R save(@RequestBody String blog){
        Blog object = JSON.parseObject(blog, Blog.class);
        if(object.getId()==null||object.getId()==0)
            return R.status(blogService.save(object));
        return R.status(blogService.updateById(object));
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable long id){
        return R.status(blogService.removeById(id));
    }

}

